home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / hurd / hurdsock.c < prev    next >
C/C++ Source or Header  |  1994-06-04  |  3KB  |  88 lines

  1. /* _hurd_socket_server - Find the server for a socket domain.
  2.  
  3. Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5.  
  6. The GNU C Library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Library General Public License as
  8. published by the Free Software Foundation; either version 2 of the
  9. License, or (at your option) any later version.
  10.  
  11. The GNU C Library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14. Library General Public License for more details.
  15.  
  16. You should have received a copy of the GNU Library General Public
  17. License along with the GNU C Library; see the file COPYING.LIB.  If
  18. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  19. Cambridge, MA 02139, USA.  */
  20.  
  21. #include <hurd.h>
  22. #include <sys/socket.h>
  23. #include <stdlib.h>
  24. #include <hurd/paths.h>
  25. #include <gnu-stabs.h>
  26. #include <stdio.h>
  27. #include "stdio/_itoa.h"
  28. #include <cthreads.h>        /* For `struct mutex'.  */
  29. #include "hurdmalloc.h"        /* XXX */
  30.  
  31. static struct mutex lock = MUTEX_INITIALIZER;
  32.  
  33. static file_t *servers;
  34. static int max_domain;
  35.  
  36. /* Return a port to the socket server for DOMAIN.
  37.    Socket servers translate nodes in the directory _SERVERS_SOCKET
  38.    (canonically /servers/socket).  These naming point nodes are named
  39.    by the simplest decimal representation of the socket domain number,
  40.    for example "/servers/socket/3".
  41.  
  42.    Socket servers are assumed not to change very often.
  43.    The library keeps all the server socket ports it has ever looked up,
  44.    and does not look them up in /servers/socket more than once.  */
  45.  
  46. socket_t
  47. _hurd_socket_server (int domain)
  48. {
  49.   socket_t server;
  50.  
  51.   HURD_CRITICAL_BEGIN;
  52.   __mutex_lock (&lock);
  53.  
  54.   if (domain > max_domain)
  55.     {
  56.       file_t *new = realloc (servers, (domain + 1) * sizeof (file_t));
  57.       if (new == NULL)
  58.     {
  59.       server = MACH_PORT_NULL;
  60.       goto out;
  61.     }
  62.       while (max_domain < domain)
  63.     new[max_domain++] = MACH_PORT_NULL;
  64.       servers = new;
  65.     }
  66.  
  67.   {
  68.     char name[sizeof (_SERVERS_SOCKET) + 100];
  69.     char *np = &name[sizeof (name)];
  70.     *--np = '\0';
  71.     np = _itoa (domain, np, 10, 0);
  72.     *--np = '/';
  73.     np -= sizeof (_SERVERS_SOCKET) - 1;
  74.     memcpy (np, _SERVERS_SOCKET, sizeof (_SERVERS_SOCKET) - 1);
  75.     server = servers[domain] = __path_lookup (np, 0, 0);
  76.   }
  77.  
  78.   if (errno == ENOENT)
  79.     /* If the server node is absent, we don't support that protocol.  */
  80.     errno = EPFNOSUPPORT;
  81.  
  82.  out:
  83.   __mutex_unlock (&lock);
  84.   HURD_CRITICAL_END;
  85.  
  86.   return server;
  87. }
  88.